package com.clw.template.wx.controller;

import com.clw.template.wx.service.IWxAuthService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpOAuth2AccessToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Api(tags = "WxAuthController",description = "微信认证管理")
@RestController
@RequestMapping("/wx/auth")
public class WxAuthController {

    @Autowired
    private IWxAuthService wxAuthService;
    @Autowired
    private WxMpService wxMpService;

    @ApiOperation("登录以后返回token")
    @PostMapping("authorize")
    public String authorize(@RequestParam("returnUrl") String returnUrl){
        String url = "";
        String redirectURL = wxAuthService.authorize(returnUrl);
        return "redirect:"+redirectURL;
    }

    @ApiOperation("登录以后返回token")
    @GetMapping("userInfo")
    public String userInfo(@RequestParam(value = "signature") String signature,
                           @RequestParam(value = "nonce" ) String nonce,
                           @RequestParam(value = "echostr" ) String echostr,
                           @RequestParam(value = "timestamp" ) long timestamp) throws Exception {
        System.out.println(signature+":"+nonce+":"+echostr+":"+timestamp);
        return echostr;
    }

    /*@ApiOperation("登录以后返回token")
    @GetMapping("userInfo")
    public String userInfo(@RequestParam(value = "code",required = false) String code,
                           @RequestParam(value = "state" ,required = false) String returnUrl) throws Exception {

        return "";
    }*/

}
